/***斗地主带机器人座位数据类
 *  
 */
var DdzhSeatBase = require('./DdzhSeatBase');
var EnumRole = require('./../../PkCommon/Enum/EnumRole');
var EumPkGroup = require('./../../PkCommon/Enum/EumPkGroup');
var _ = require('./../../FrameWork/we_underscore');
var ClientEveDelegate = require('../../FrameWork/Delegate/ClientEveDelegate');
var ClientPkDelegateType = require('../../PkCommon/Delegate/ClientPkDelegateType');
var AIGetCard = require('../../PkCommon/AI/Get/AIGetCard');
var AIOutCard = require('../../PkCommon/AI/Out/AIOutCard');
var PkCardTool = require('./../../PkCommon/FindTool/PkCardTool');
var MgTimeOut = require('../../FrameWork/Manager/MgTimeOut');
module.exports = cc.Class({
    extends: DdzhSeatBase,
    ctor() {
        this.createName();
        // this.setMoney(_.random(1000, 50000));
        this.roleType = EnumRole.ROBOT; //标记为机器人
        this.cardLeftCnt = 0; //剩余的牌的数量
        this.sex = _.random(1, 2);
        this.handCards = []; //手牌
    },
    /***生成一个机器人 */
    createName() {
        var name = 'robot_' + (_.random(1, 9999)).toString();
        this.setPlayName(name);
    },
    /***手牌 */
    setHandCards(_handCards) {
        this.handCards = _handCards
    },
    /***手牌 */
    getHandCards() {
        return this.handCards;
    },
    /**往手牌增加牌 */
    addHandCards(_cardsId) {
        this.handCards.push(_cardsId);
        this.handCards = _.flatten(this.handCards);
    },
    /**
     * 智能ai接口
     * 处理过还是要起
     */
    doAI(_oldOutCardInfo) {
        //cc.error('doAI'); 
        ClientEveDelegate.doDataToLister(ClientPkDelegateType.Clinet_showOtherTimeOut, {
            seatIdx: this.seatIdx
        });


        var timeOutKey = MgTimeOut.do(function() {
            if (window.g_room) {
                if (false == window.g_room.autoCheckIsGet()) {
                    window.g_room.noOut({
                        seatIdx: this.seatIdx
                    })
                    return;
                }
                var result = AIGetCard.doAI(this.handCards, _oldOutCardInfo);
                if (result.isFind) {
                    var cardsInfo = result.recommendCards;
                    if (!cardsInfo) {
                        cardsInfo = PkCardTool.getCardInfos(cardIds);
                    }
                    var cardIds = _.pluck(cardsInfo, 'id');
                    var info = {
                        seatIdx: this.seatIdx,
                        type: result.type,
                        cardIds: cardIds,
                        cardsInfo: cardsInfo
                    };
                    window.g_room.outCardAct(info);
                } else {
                    window.g_room.noOut({
                        seatIdx: this.seatIdx
                    })
                }
                //cc.error('执行机器人AI处理');
            }
            // }.bind(this), _.random(2000, 5500));
        }.bind(this), _.random(1000, 1000));
        return timeOutKey;
    },

    /***机器人出任意牌型 */
    doAIRandOutCard() {
        var timeOutKey = MgTimeOut.do(function() {
            if (window.g_room) {
                var result = AIOutCard.doAI(this.handCards);
                window.g_room.outCardAct({
                        seatIdx: this.seatIdx,
                        type: result.type,
                        cardIds: result.cardIds,
                        cardsInfo: PkCardTool.getCardInfos(result.cardIds),
                        isFristOutCard: true
                    })
                    //cc.error('机器人出任意牌型');
            }
            // }.bind(this), _.random(2000, 5500));
        }.bind(this), _.random(1000, 1000));


    },
    /***AI叫分 */
    doAICallScore() {
        ClientEveDelegate.doDataToLister(ClientPkDelegateType.Clinet_showOtherTimeOut, {
            seatIdx: this.seatIdx
        });

        var timeOutKey = MgTimeOut.do(function() {
            if (window.g_room) {
                window.g_room.refreshCallScore({
                    seatIdx: this.seatIdx,
                    score: _.random(0, 3)
                });
            }
        }.bind(this), _.random(1, 2));
        // }.bind(this), _.random(5000, 29000));
        return timeOutKey;
    },

    /***AI加倍*/
    doAIDuble() {
        ClientEveDelegate.doDataToLister(ClientPkDelegateType.Clinet_showOtherTimeOut, {
            seatIdx: this.seatIdx
        });

        var timeOutKey = MgTimeOut.do(function() {
            if (window.g_room) {
                window.g_room.refreshDoubleInfo({
                    seatIdx: this.seatIdx,
                    double: _.random(0, 1)
                });
            }
            // }.bind(this), _.random(2000, 3500));
        }.bind(this), _.random(1, 2));
        return timeOutKey;
    },
    /***出牌动作
     * _outCardInfo:{seatIdx:1,groupType:1,cardIds:[]}
     */
    outCardAct(_outCardInfo) {
        this._super(_outCardInfo);
        var outCardSize = _.size(_outCardInfo.cardIds);
        var outCards = _outCardInfo.cardIds;
        this.handCards = _.difference(this.handCards, outCards);

        var cnt = _.size(this.handCards);
        return this.refreshCardLeftCnt(cnt);
        // return this.refreshCardLeftCnt(this.cardLeftCnt - outCardSize);
        //自己要做其他家手牌数量变更处理
    },
    //刷新手牌数量
    refreshCardLeftCnt(_cardLeftCnt) {
        this._super(_cardLeftCnt);
        this.cardLeftCnt = _cardLeftCnt;
        ClientEveDelegate.doDataToLister(ClientPkDelegateType.Clinet_refreshCardLeftCnt, {
            seatIdx: this.seatIdx,
            cardLeftCnt: this.cardLeftCnt
        });
        return this.cardLeftCnt;
    },
    getCardLeftCnt() {
        this._super();
        return this.cardLeftCnt;
    },
    getSeatInfo() {
        var info = this._super();
        info.cardLeftCnt = this.getCardLeftCnt();
        info.handCards = this.handCards;
        return info;
    }
});